package cn.wlh.learn.webmagic.selenium.discuz;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.CalendarUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.system.SystemUtil;
import cn.wlh.learn.webmagic.pipeline.*;
import cn.wlh.learn.webmagic.tool.AliOssClientTool;
import cn.wlh.learn.webmagic.utils.ArrayUtils;
import cn.wlh.learn.webmagic.utils.ForEachUtils;
import cn.wlh.learn.webmagic.utils.SelectableUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * webMagic获取cookie登陆
 * https://blog.csdn.net/weixin_42244754/article/details/81540678
 * ，账户过期了，没法用了。爬出来了，但是没法用了。
 * Selenium
 */
public class LongFengYouSelenium  extends AbstractDiscuzSelenium{
    private final static Log logger = LogFactory.get();


    public LongFengYouSelenium() {
        super(
                "https://www.52lfy.com");
    }


    @Override
    public void viewthread(Page page) {
        // 抓取规则
        Selectable url = page.getUrl();
//        logger.warn("url=[{}]", url);


        //列表页
        Html html = page.getHtml();


        // //*[@id="aimg_7996"]
//        logger.debug("详情页面内容=[{}]", html);
        // 获得图片.没有就不记录.
//        Selectable css1 = html.css("div.mbn.savephotop > img");
        // 获得相对路径， links 获得不到里面的内容。
//        Selectable zoomfileSelect = html.css("div.mbn.savephotop > img","zoomfile");
        Selectable zoomfileSelect = html.css("img","zoomfile");
        List<String> zoomfileList = zoomfileSelect.all();
        // 过滤空白的图片
        zoomfileList = zoomfileList.stream().filter(zoom -> StrUtil.isNotBlank(zoom)).collect(Collectors.toList());
        //
        List<String> images = ForEachUtils.format(this.getIndex_url()+"/",zoomfileList);;
//            boolean empty = CollectionUtil.isEmpty(images);

//        Selectable xpath = html.xpath("//div[@align='left']");
        Selectable xpath = html.xpath("//td[@class='t_f']");
        List<String> all = xpath.all();
        //
        List<String> body = all.stream().map(left -> HtmlUtil.cleanHtmlTag(left)).filter(str -> StrUtil.isNotBlank(str)).collect(Collectors.toList());
        //
        page.putField("images", new JSONArray(images));
        page.putField("body", new JSONArray(body));

        String title = html.xpath("//html/head/title/text()").get();
        page.putField("title",title);

//            page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2"));
//            page.putField("content", page.getHtml().xpath("//div[@id='articlebody']//div[@class='articalContent']"));
//            page.putField("date",
//                    page.getHtml().xpath("//div[@id='articlebody']//span[@class='time SG_txtc']").regex("\\((.*)\\)"));
    }


    @Override
    public void login(String useName, String pass) {

          /*
          登录
           */
//        int inputMillis = 3000;
//        this.login(useName,pass, inputMillis,inputMillis * 10);


        /*
         * 生成的代码手动赋值。
         */
        Site site = this.site;
        site.addCookie("FRWN_2132_lastcheckfeed", "16868%7C1629879518");
        site.addCookie("FRWN_2132_checkfollow", "1");
        site.addCookie("FRWN_2132_seccodecSAmRHIZ4FfF", "1936.6e5ee026d8e8488d14");
        site.addCookie("FRWN_2132_lastvisit", "1629875904");
        site.addCookie("FRWN_2132_sid", "IZ4FfF");
        site.addCookie("FRWN_2132_noticeTitle", "1");
        site.addCookie("FRWN_2132_nofavfid", "1");
        site.addCookie("FRWN_2132_checkpm", "1");
        site.addCookie("FRWN_2132_auth", "b317bFzWbdyBeF1MKwm3oz5bsS47nk3LYo4ASzTd8MINevSr1swg2SYWCvsiYIDxQC%2FG%2BKC0o4CRD5uwxsgj4R7nPQ");
        site.addCookie("FRWN_2132_sendmail", "1");
        site.addCookie("FRWN_2132_saltkey", "s4z4x7o6");
        site.addCookie("FRWN_2132_lip", "114.240.86.222%2C1629879212");
        site.addCookie("FRWN_2132_nofocus_forum", "1");
        site.addCookie("FRWN_2132_ulastactivity", "1ac0bDRkuPolP6sMRCWjb3iUfM%2BddYLQVNfUAplbepMFZ3nHGETi");
        site.addCookie("FRWN_2132_lastact", "1629879522%09misc.php%09patch");
        System.out.println(site.getCookies());
    }

    public static void main(String[] args) throws SQLException {

//
//        String post = HttpUtil.post("https://c315e50f-51ea-42ca-b5f6-67052648e4f1.bspapp.com/http/bbs_topic?cloudAction=save", "{\"a\":1,\"b\":2}");
//        System.out.println(post);
//        if(true)
//            return;


        LongFengYouSelenium dome = new LongFengYouSelenium();
        // 爬取url地址。
        String url = dome.getIndex_url() + "/forum.php?mod=forumdisplay&fid=76&page=1";

        /**
         * 组装存储Pipeline管道。
         */
        // 插入到。sheji_caiji_piaochong 表中.
        DBPipeline dBPipeline = new DBPipeline("caiji_lfy");

        String images_flag = "images";
        // 本地存一份图片，用于审计敏感图片。
        PicPipeline picPipeline = new PicPipeline(images_flag, images_flag, "E:\\Projects\\lunlv\\yunlv-owl\\lfy");
        // 上传到oss
        AliOssPipeline aliOssPipeline = new AliOssPipeline(images_flag, images_flag, new AliOssClientTool("yunlv-owl"), "lfy/");
        aliOssPipeline.setShowEndpointKey("endpoint");
        // 使用的时候再设置，先保存相对路径。   设置图片一个月的有效期。
//        Calendar now = CalendarUtil.calendar();
//        now.add(Calendar.MONTH,1);
//        aliOssPipeline.setExpiration(now);
//


        /**
         * 开始爬取。
         */
        Spider.create(dome).addUrl(url)
                .addPipeline(picPipeline)    // 存本地
                .addPipeline(aliOssPipeline)  // 存oos
                .addPipeline(new ClassConvertPipeline())  // 类型转化
                .addPipeline(dBPipeline)   // 存DB
                .addPipeline(new LoggerPipeline(logger)) // 打印
                .thread(1).run();

    }
}